<template>
  <!-- <el-button type="primary" @click="myFn">获取所有用户信息</el-button> -->
  <el-container>
    <el-header>
      <div class="header-left" @click="toggleCollapse"></div>
      <el-dropdown>
        <div class="header-right">
          <img :src="userInfo.baseURL + userInfo.avatarURL" alt="" />
          <p>{{userInfo.name}}</p>
          <i class="el-icon-arrow-down el-icon--right"></i>
        </div>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item>首页</el-dropdown-item>
          <el-dropdown-item divided @click.native="outLogin"
            >退出登录</el-dropdown-item
          >
        </el-dropdown-menu>
      </el-dropdown>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse ? '65px' : '200px'">
        <el-menu
          class="el-menu-vertical-demo"
          background-color="#fff"
          text-color="#666"
          active-text-color="deepskyblue"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="defaultActivePath"
        >
          <!-- --一级菜单-- -->
          <el-submenu
            :index="item.menuName"
            v-for="item in menus"
            :key="item.menuName"
          >
            <template slot="title">
              <i :class="item.icon"></i>
              <span>{{ item.menuName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item-group>
              <el-menu-item
                :index="subItem.path"
                v-for="subItem in item.children"
                :key="subItem.menuName"
                @click="changeDefaultActivePath(subItem.path)"
              >
                <template slot="title">
                  <i :class="subItem.icon"></i>
                  <span>{{ subItem.menuName }}</span>
                </template>
              </el-menu-item>
            </el-menu-item-group>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script lang="ts">
import { text,getOneUserInfo } from "@/api";
import Cookies from "js-cookie";
import { Component, Vue } from "vue-property-decorator";
@Component({
  name: "Admin",
  components: {},
})
export default class Admin extends Vue {
  private defaultActivePath = ''
  private menus = [
    {
      menuName: "用户管理",
      path: "",
      icon: "el-icon-setting",
      children: [
        {
          menuName: "用户列表",
          path: "/users",
          icon: "el-icon-user",
          children: [],
        },
      ],
    },
    {
      menuName: "权限管理",
      path: "",
      icon: "el-icon-collection",
      children: [
        {
          menuName: "角色列表",
          path: "/roles",
          icon: "el-icon-s-custom",
          children: [],
        },
        {
          menuName: "权限列表",
          path: "/rights",
          icon: "el-icon-unlock",
          children: [],
        },
      ],
    },
  ];
  private id:any = localStorage.getItem('id')
  private userInfo = {}
  private isCollapse = false;
  myFn() {
    text()
      .then((data) => {
        console.log(data);
      })
      .catch((e) => {
        console.log(e);
      });
  }
  // 点击切换侧边栏收缩
  private toggleCollapse() {
    this.isCollapse = !this.isCollapse;
  }
  // 点击选择默认高亮的菜单
  private changeDefaultActivePath(path:string){
    this.defaultActivePath = path
    // 存储到本地
    sessionStorage.setItem('activePath',path)
  }
  // 在页面创建的时候从本地取出来赋值给 defaultActivePath
  async created() {
    const path = sessionStorage.getItem('activePath')
    this.defaultActivePath = path ? path : ''
    // 获取用户信息
    const result:any = await getOneUserInfo(this.id)
    // console.log(result);
    this.userInfo = result.data.data
  }
  // 退出登录
  private outLogin() {
    sessionStorage.removeItem('activePath')
    Cookies.remove("token");
    localStorage.removeItem('id')
    this.$router.push("/login");
  }
}
</script>

<style lang="scss" scoped>
.el-container {
  background: #fff;
  width: 100%;
  height: 100%;
  .el-header {
    background-color: deepskyblue;
    display: flex;
    justify-content: space-between;
    .header-left {
      width: 200px;
      height: 60px;
      background: url("../assets/common/logo.png") no-repeat;
      background-size: 80% auto;
    }
    .header-right {
      display: flex;
      justify-content: space-between;
      align-items: center;
      line-height: 30px;
      img {
        width: 40px;
        height: 40px;
        border-radius: 50%;
      }
      p {
        padding: 0 10px;
      }
    }
  }
  .el-aside {
    background-color: #fff;
    i{
      color:#43a7fe !important;
    }
  }
}
</style>
